package com.zhu.ssm.service;

import com.zhu.ssm.mapper.UserMapper;
import com.zhu.ssm.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public User findUser(Long id){
        return userMapper.findUser(id);
    }

    /**
     * 这里的事务出现了bug 如果这个user直接在里面声明，事务失效，必须走controller
     * 例如：User user=new User();
     *         user.setAge(20);
     *         user.setEmail("张三");
     *         user.setName(“张三”);
     *         失效，必许让参数走controller
     * @param age
     * @param email
     * @param name
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(Integer age,String email,String name){
        User user=new User();
        user.setAge(age);
        user.setEmail(email);
        user.setName(name);
        userMapper.save(user);
//        int i=1/0;
        System.out.println(user.getId());
    }
}
